package com.crm.constant.exception;


import com.crm.constant.BusinessException;
import com.crm.constant.dto.BaseFxResponse;
import com.crm.constant.dto.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : CRM_SYSTEM
 * @date : 2022/03/28 14:07
 */
@RestControllerAdvice
@Slf4j
@Order(1)
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseFxResponse<Object> resolveException(Exception ex) {
        log.error("系统异常:{}", ex);
        //判断异常类型
        if (ex instanceof BusinessException) {
            BusinessException exception = (BusinessException) ex;
            return BaseFxResponse.result(exception.getExceptionCode(), exception.getException());
        }

        if (ex instanceof MethodArgumentNotValidException) {
            return BaseFxResponse.fail(ResultCode.EXCEPTION_NO_PARAMETER);
        }
        return BaseFxResponse.fail(ResultCode.EXCEPTION);
    }

}
